﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.KyThiService;
namespace eTMS.Controllers{
public class KyThiControllerBase : CommonController{
	public KyThiControllerBase()
		: base()
	{	
		this.dao = new KyThiServiceClient();
	}
	~KyThiControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private KyThiEntity oKyThi;	
	public KyThiEntity KyThiObject{
		get{
			return oKyThi;
		}
		set{
			oKyThi=value;
		}
	}
	protected KyThiServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSKyThi(false);			
			return _Table;
		}
	}
	public void LoadAllDSKyThi(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oKyThi==null)return false;
		bool result=true;
		try{
			oKyThi = dao.SaveRet(oKyThi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListKyThi.Add(oKyThi);
		}
		return result;
	}
	public bool Update(){
		if(oKyThi==null)return false;
		return dao.Update(oKyThi);
	}
	public bool Delete(){
		if(oKyThi==null)return false;
		bool retvalue = dao.Delete(oKyThi);
		if(retvalue){			
			_ListKyThi.Remove(oKyThi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 KyThi_ID){
		bool retvalue = dao.DeleteByID(KyThi_ID);		
		if(retvalue){
			KyThiEntity oDeleted=FindKyThi(KyThi_ID);
			if(oDeleted!=null){
				_ListKyThi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oKyThi = new KyThiEntity();
	}
    //Copy data from data row in grid to object
    public KyThiEntity MapDataRowToObject(DataRow row){
		KyThiEntity o = new KyThiEntity();
		 o.TenKyThi=Convert.ToString(row["TenKyThi"]);
		 if(!row.IsNull("TrangThai"))
			o.TrangThai=ConvertByte(row["TrangThai"]);
		 if(!row.IsNull("HocKy_ID"))
			o.HocKy_ID=(Int32)(row["HocKy_ID"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 if(!row.IsNull("KyThi_ID"))
		    o.KyThi_ID=(Int32)(row["KyThi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(KyThiEntity o,DataRow row){
		row["TenKyThi"]=o.TenKyThi;
		row["TrangThai"]=o.TrangThai;
		row["HocKy_ID"]=o.HocKy_ID;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["KyThi_ID"]=o.KyThi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.KyThiObject,row);
    }
	public void LoadObject(Int32 KyThi_ID){
		KyThiEntity oLoad=FindKyThi(KyThi_ID);
		if(oLoad==null){
			oKyThi=this.LoadKyThi(KyThi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oKyThi.IsNullKyThi_ID;
			if(!bNotIsNull){
				this.ListKyThi.Add(oKyThi);
			}
		}
		else{
			oKyThi=oLoad;
		}
	}
	#region CachedList KyThi
	private static List<KyThiEntity> _ListKyThi=new List<KyThiEntity>();
    public List<KyThiEntity>ListKyThi{
		get{
			return _ListKyThi;
		}
	}
	public static List<KyThiEntity>ListKyThiStatic{
		get{
			return _ListKyThi;
		}
	}
	public KyThiEntity FindKyThi(Int32 KyThi_ID){
		KyThiEntity oResult=this.ListKyThi.Find(delegate(KyThiEntity oFind){
			bool result=true; 
			result&=oFind.KyThi_ID.Equals(KyThi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllKyThi(bool clearCache){
		if(_ListKyThi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		KyThiServiceClient dao = new KyThiServiceClient();
		_ListKyThi=new List<KyThiEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public KyThiEntity LoadKyThi(Int32 KyThi_ID){			
		KyThiEntity o=dao.SelectOne(KyThi_ID);		
		return o;
	}
	#endregion
}
}
